package com.impinj.program;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;

import com.impinj.octane.BitPointers;
import com.impinj.octane.ImpinjReader;
import com.impinj.octane.MemoryBank;
import com.impinj.octane.SequenceState;
import com.impinj.octane.TagOp;
import com.impinj.octane.TagOpSequence;
import com.impinj.octane.TagReadOp;
import com.impinj.octane.TargetTag;

public class ReadMode {
	
	private static Properties properties;
	
	// 获取默认配置
	static {
		// 从 db 获取流文件
		InputStream inputString = 
				ClassLoader.getSystemResourceAsStream("db.properties");
		// 新建 properties 对象
		properties = new Properties();
		// 尝试加载流文件
		try {
			// 加载文件
			properties.load(inputString);
			System.out.println("配置文件加载成功");
			// 获取读写器
			
		} catch (IOException e) {
			System.out.println("未发现配置文件");
		}
	}
	
	// 设置读写器的操作序列
	public static TagOpSequence creatTagOpSequence() {
		TagOpSequence seq = new TagOpSequence();
        seq.setOps(new ArrayList<TagOp>());
        // 序列重复执行的次数，0 是一直执行
        seq.setExecutionCount((short) 0); // forever
        // 是否执行这个序列
        seq.setState(SequenceState.Active);
        // 序列的 id 编号
        seq.setId(1);
        return seq;
	}
	
	// 设置读取 user 区
	public static TagReadOp creatTagReadOp() {
		TagReadOp readOp = new TagReadOp();
        // 选择读 User 区信息
        readOp.setMemoryBank(MemoryBank.User);
        // 读前 x 组的信息
        readOp.setWordCount((short) 6);
        // 设置 User 的起始位置
        readOp.setWordPointer((short) 0);
        return readOp;
	}
	
	// 配置读操作的设置
	public static TagOpSequence configReadMode(ImpinjReader reader, TagOpSequence seq) {
        // 设置 seqRecognizeTheRoom 的操作序列
        String targetEpc = System.getProperty(SampleProperties.targetTag);
        if (targetEpc != null) {
            seq.setTargetTag(new TargetTag());
            seq.getTargetTag().setBitPointer(BitPointers.Epc);
            seq.getTargetTag().setMemoryBank(MemoryBank.Epc);
            seq.getTargetTag().setData(targetEpc);
            return seq;
        } else {
            // or just send NULL to apply to all tags
            seq.setTargetTag(null);
             return seq;
        }
	}
} 
